/**
 * 在其他对象上同步
 *
 * @author fireway
 * @since 2019年 01月 09日 星期三 06:40:56 CST
 */
package concurrency;

class DualSynch {
    private Object mSyncObject = new Object();

    public void go() {
        synchronized (mSyncObject) {
            for (int i = 0; i < 5; i++) {
                System.out.println("g()");
                Thread.yield();
            }
        }
    }

    public synchronized void f() {
        for (int i = 0; i < 5; i++) {
            System.out.println("f()");
            Thread.yield();
        }
    }
}

public class SyncObject {
    public static void main(String[] args) {
        final DualSynch ds = new DualSynch();

        new Thread() {
            @Override
            public void run() {
                ds.f();
            }
        } .start();

        ds.go();
    }
}
